package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.threadLocal.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.CountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;


    /**
     * 查询取消预约数量
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        int count = reservationService.getCancelledReservationCount(userId);
        return R.ok(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     */
    @GetMapping("/countByTime")
    public R<List<CountVo>> getReservationCountByTime(Long time) {
        //Long userId = UserThreadLocal.getUserId();
        List<CountVo>  count = reservationService.getReservationCountByTime(time);
        return R.ok(count);
    }


    /**
     * 新增预约
     */
    @PostMapping
    @ApiOperation("新增预约")
    public AjaxResult add(@RequestBody ReservationDto dto){
        int i = reservationService.insertReservation(dto);
        return i > 0 ? success() : error();
    }

    /**
     * 查询预约信息列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取预约信息列表")
    public AjaxResult list(Reservation reservation)
    {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 2.5 取消预约
     */
    @PutMapping("/{id}/cancel")
    @ApiOperation(value = "取消预约")
    public AjaxResult cancel(@PathVariable Long id) {
        boolean b =reservationService.cancel(id);
        return b ? success() : error();
    }
}